package com.tj.bigdata.syncblock.producer;

import com.alibaba.fastjson.JSON;
import com.tj.bigdata.syncblock.constant.KafkaConstant;
import com.tj.bigdata.syncblock.entity.BlockInfo;
import com.tj.bigdata.syncblock.entity.Transactions;
import com.tj.bigdata.syncblock.entity.TransactionsStreamSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * 发送区块消息Kafka
 *
 * @author guoch
 */
@Component
@Slf4j
public class BlockChainInfoKafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendBlock(BlockInfo ethBlock) {
        kafkaTemplate.send(KafkaConstant.KAFKA_BLOCK_INFO, JSON.toJSONString(ethBlock)).addCallback(new CallBackService());
    }

    public void sendTransaction(Transactions transactions) {
        kafkaTemplate.send(KafkaConstant.KAFKA_TRANSACTION_TOPIC, JSON.toJSONString(transactions)).addCallback(new CallBackService());
    }

    public void sendTransactionStreamSource(TransactionsStreamSource streamSource) {
        kafkaTemplate.send(KafkaConstant.KAFKA_TRANSACTION_STREAM_SOURCE_TOPIC, JSON.toJSONString(streamSource)).addCallback(new CallBackService());
    }
}
